/*******************************************************************************
 Jimm - Mobile Messaging - J2ME ICQ clone
 Android version
 Copyright (C) 2003-11  Jimm Project

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
 File: src/jimm/app/InitializeActivity.java
 Version: ###VERSION###  Date: ###DATE###
 Author(s): Ashikhmin Kirill
 *******************************************************************************/
package org.jimm.app;

import org.jimm.db.JimmDatabase.Accounts;
import org.jimm.service.Preferences;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;


public class InitializeActivity  extends Activity {
	
	public static final String LOG_TAG = "Jimm:InitializeActivity";
	private SharedPreferences preferences;
	public static boolean license;
	static ContentResolver contentResolver;	
	Context context;
	
	@Override
	protected void onCreate(Bundle init) {
	super.onCreate(init);
	setContentView(R.layout.loading);	
	contentResolver = getContentResolver();
	
	JimmApplication.appStarted=true;
		
	context=getApplicationContext();

	//TODO выбор языка
	/*
	1. create a normal android
	dialog with radio button selection of English &
	German 
	2. OnCheckedChange ()
	write this to change the Language Locale
	myLocale = new Locale(/String selected/);
	Locale.setDefault(myLocale );
	Configuration config2 = new Configuration();
	config2.locale = myLocale ; getBaseContext
	().getResources().updateConfiguration(config2,
	getBaseContext().getResources
	().getDisplayMetrics());
	
	protected void onResume() {
		super.onResume();
		Log.i(LOG_TAG, "onResume"); 
		
		preferences = PreferenceManager.getDefaultSharedPreferences(this);
	    lang = preferences.getString(org.jimm.service.Preferences.PREF_LANG, getResources().getConfiguration().locale.getDisplayName());
	    Log.i(LOG_TAG, "Lang="+getResources().getConfiguration().locale.getDisplayName());
		Log.i(LOG_TAG, "LangR="+lang);
		if (lang != null) {
		Locale myLocale = new Locale(lang);
		Locale.setDefault(myLocale);
		Configuration config2 = new Configuration();
		config2.locale = myLocale;
		getBaseContext().getResources().updateConfiguration(config2,getBaseContext().getResources().getDisplayMetrics());
		Log.i(LOG_TAG, "6LangSys="+getResources().getConfiguration().locale.getDisplayName());
		} else {Log.i(LOG_TAG, "Lang=Fucking null!!");}
	}
	@Override
    public void onConfigurationChanged(Configuration newConfiguration)
    {
        super.onConfigurationChanged(newConfiguration);
    }
	
			*/
	preferences = PreferenceManager.getDefaultSharedPreferences(this);
	 boolean license = preferences.getBoolean(Preferences.PREF_LICENSE, false);
   Log.i(LOG_TAG, "License="+license);

  // license=true;
	if (!license) {
	startActivity(new Intent(this, LicenseActivity.class));
	} else
	{
		startActivity(new Intent(this, ContactListActivity.class));		
		//startActivity(new Intent(this, SmileActivity.class));
		if (needAcc(context)) {startActivity(new Intent(this, AccountsEditorActivity.class));};
	}	
	finish();
	}
	
	public static boolean needAcc (Context context) {
		Cursor c = contentResolver.query(Accounts.CONTENT_URI,new String[] {Accounts._ID}, null, null, null);
		long accCount=c.getCount();
		c.close();
		
		Log.i(LOG_TAG,"accCount="+accCount);
		
		if (accCount<1) {
			Toast.makeText(context, context.getText(R.string.NeedAccount),Toast.LENGTH_SHORT).show();
			return true;
		} else return false;
		}
	
}
